/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "checkbatteryinstructionlogic.h"
#include "GeneralDefine.h"
#include "programengine.h"
#include "../BatteryMonitor/batterymonitor.h"

extern BatteryMonitor *g_batteryMonitor;
CheckBatteryInstructionLogic::CheckBatteryInstructionLogic(int robotIdIn,ProgramSet* programSetIn,
                                             RobotIo* ioControllerIn,ProgramEngine* programEngineIn)
  {
      programSet=programSetIn;
      robotId=robotIdIn;
      ioController=ioControllerIn;
      programEngine=programEngineIn;
  }

E_PROGRAM_LOOP_RESULT CheckBatteryInstructionLogic::startCheckBatteryLevel(int programNumIn, const CheckBatteryInstruction &instructionIn, int lineIndex)
{
    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;

    if(NULL==g_batteryMonitor || false ==g_batteryMonitor->isInitalOk())
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3944, robotId
                , programEngine->getRunningLineFloat() );
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    //++labelCount;
    if(instructionIn.instructionType != CHECK_BATTERY)
    {
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }
    else
    {
        // 获取电池组状态信息
        if(g_batteryMonitor->getBatteryInfo(batteryInformation))
        {
            qDebug() << QObject::tr("CheckBatteryInstruction ==== 电池组状态信息获取成功!");

            int valueFromInstructin = instructionIn.electricQuantity;
            int valueFromBatteryInfo = static_cast<int>(batteryInformation.batteryLeftLightmodRatio*100);
            if(valueFromInstructin >= valueFromBatteryInfo)
            {
                //电量过低跳转 当前跳转指令为索引使用int类型，后续跳转指令索引将使用QString类型
                return programEngine->startJump(programNumIn,instructionIn.labelIndex);
            }
        }
        else
        {
            // 电池组状态信息获取失败
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  E_ERROR_GETBATTERYINFO_FAILD, robotId
                    , programEngine->getRunningLineFloat() );
        }

    }

    programEngine->calculateNextLineToRun(programNumIn);
    endFlag = programEngine->isTheLastInstruction(programNumIn);

    return endFlag;

    //    qDebug() << "startLabel*****3***programSet.pc"<<(*programSet)[programNum].pc<<"programNum***********"<<programNum<<endl;
}
//E_PROGRAM_LOOP_RESULT CheckBatteryInstructionLogic::startJump(int programNum,int jumpNum)
//{
////    qDebug() << __FUNCTION__<<"***line***"<<__LINE__<<"==r="<<programSet->r[0]<<"===="<<programSet->r[1]<<"==="<<programSet->r[2]<<endl;
//    programEngine->updateProgramSetRegister();

//    if( programEngine->getRunModel()<ENUM_CONTROL_AUTO && programEngine->getMoveType()==ENUM_MOVE_ORDER_STEP)
//    {
//        programEngine->set_mamual_run_flag(false);
//    }
//    E_PROGRAM_LOOP_RESULT endFlag = E_PROGRAM_LOOP_RESULT_NORMAL;
//    int tmpJumpNum;
//    for(int i = 0; i < (*programSet)[programNum].count();i++)
//    {
//        if((*programSet)[programNum].at(i).type == LABEL)
//        {
//            switch((*programSet)[programNum].at(i).label.type)
//            {
//            case DIRECT:
//            {
//                tmpJumpNum = (int)(*programSet)[programNum].at(i).label.i;
//                //                    qDebug() << __FUNCTION__<<"***line***"<<__LINE__<<"**DIRECT**tmpJumpNum***"<<tmpJumpNum<<endl;
//                break;
//            }
//            case INDIRECT:
//            {
//                tmpJumpNum = (int)programSet->regRList.at((*programSet)[programNum].at(i).label.i -1);
//                //                                    qDebug() << __FUNCTION__<<"***line***"<<__LINE__<<"**INDIRECT**tmpJumpNum***"<<tmpJumpNum<<"==i="<<(*programSet)[programNum].at(i).label.i<<endl;
//                break;
//            }
//            }

//            if(jumpNum == tmpJumpNum)
//            {
//                (*programSet)[programNum].runningLineInt = i;
////                pCount = (*programSet)[programNum].pc+0.02;
//                programEngine->setPcFlagValue(0.0);
//                break;
//            }
//            else
//            {
//                if(i == (*programSet)[programNum].count() - 1)//查找到最后一行，发现没有与之匹配的ｌａｂｅｌ标签，报错处理
//                {

//                 //   MessageLog::getInstance()->addMessage( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003, robotId );
//                    addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003,
//                            robotId,(*programSet)[programNum].runningLineInt,0,0,0,programEngine->getCurrentProgramName() );
//                    return E_PROGRAM_LOOP_RESULT_ERROR;
////                    break;
//                }
//                else
//                {
//                    continue;
//                }
//            }
//        }
//        else
//        {
//            if( i == (*programSet)[programNum].count() - 1)
//            {

//             //   MessageLog::getInstance()->addMessage( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003, robotId );
//                addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  3003, robotId );
//                programEngine->cmdError(programNum);
//                qDebug()<<"error:  没找到匹配的ｌａｂｅｌ";
//                return E_PROGRAM_LOOP_RESULT_ERROR;
//            }
//        }
//    }

//    endFlag = programEngine->isTheLastInstruction(programNum);

//    return endFlag;

//    //    qDebug() << "startJump*****2222222222***programSet.pc"<<(*programSet)[programNum].pc<<"programNum***********"<<programNum<<endl;

//}

void CheckBatteryInstructionLogic::addMsg(int messageLevel, string componentName, string messageType,
                           int messageCode, int robotId,
                           int parameter1, int parameter2, int parameter3, int parameter4,QString message)
{
    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;
   // tmpMsg.time = t1.tv_sec;


    QString infomationStr;

    switch(messageCode)
    {

    case 3944:
    {

       infomationStr = QObject::tr("机器人:") +
               QString::number(robotId) +
               QObject::tr(": 电池通讯没有启用！")+message;

        break;
    }
    case 0:
    {
        infomationStr =  QObject::tr("机器人:")  + QString::number(robotId);
        infomationStr = infomationStr + QObject::tr("; %1,第%2行， IO号超出已有逻辑ＩＯ范围，请检查指令或ＩＯ配置，").arg(message).arg(parameter3)
                +QString::number(parameter1) + ">="
        +QString::number(parameter2);
        break;


    }



    default:
    {

    }
    }


    tmpMsg.MessageInformation = infomationStr.toStdString();

    // qDebug()<<"53444444444444444444444"<< tmpMsg.messageCode;
    MessageLog::getInstance()->addMessage(tmpMsg);
}
